package com.topdt.jbpm.web.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.thx.common.struts2.BaseAction;
import com.thx.common.util.AppContext;

@Namespace("/workflow")
@Action(value = "download",
		results = { @Result(name = "success", type = "stream", params = {
		"contentType", "application/octet-stream;charset=ISO8859-1","inputName", "inputStream",
		"contentDisposition","attachment;filename=${downloadFileName}", "bufferSize", "4096" }) 
		}
)
@Controller
@Scope("prototype")
public class DownloadAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;

	private String inputPath;

	private Long personnelId;
	
	private boolean isAbsolutePath;

	private String downFileName;
	public void setDownFileName(String downFileName) {
		this.downFileName = downFileName;
	}

	public String getInputPath() {
		return inputPath;
	}

	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}

	public void setInputPath() {
	}

	public String getDownloadFileName() {
		logger.debug("<进入>:getDownloadFileName方法   获取下载文件名");
		try {
			if(StringUtils.isNotBlank(downFileName)){
				downFileName = new String(downFileName.getBytes("gb2312"), "ISO8859-1");
			}
		} catch (UnsupportedEncodingException e) {
			logger.error("<错误>:获取下载文件名失败",e);
		}
		return downFileName;
	}

	public InputStream getInputStream() throws Exception {
		String mainUploadPath = AppContext.getProperties("uploadPath");
		File file;
		if(isAbsolutePath){
			file = new File(inputPath);
		}else{
			file = new File(mainUploadPath+inputPath);
		}
		FileInputStream inputStream = new FileInputStream(file);

		return inputStream;
	}

	public String execute() throws Exception {
		logger.debug("<进入>:execute方法");
		return SUCCESS;
	}


	public Long getPersonnelId() {
		return personnelId;
	}

	public void setPersonnelId(Long personnelId) {
		this.personnelId = personnelId;
	}

	public String getDownFileName() {
		return downFileName;
	}

	public boolean getIsAbsolutePath() {
		return isAbsolutePath;
	}

	public void setIsAbsolutePath(boolean isAbsolutePath) {
		this.isAbsolutePath = isAbsolutePath;
	}

}
